# -*- coding: utf-8 -*-
# @Author	: changfeng
# @Date		: 2025/9/13 17:06
# @Last Modified by:   changfeng
# @Last Modified time: 2025/9/13 17:06
# Thanks for your comments!


from typing import Optional, List

from bson import ObjectId
from pymongo import UpdateOne

from src.basedata.domain.po import ApplicationMedicineLibraryMapPO
from src.basedata.domain.repositories import AppMedicineLibraryMappingAbstractRepository
from src.basedata.infrastructure.client.mongodb.repository import MongoDBRepository
from src.basedata.infrastructure.utils import const


class MotorAppMedicineLibraryMappingRepository(AppMedicineLibraryMappingAbstractRepository, MongoDBRepository):
    def __init__(self, session):
        MongoDBRepository.__init__(self, session, const.AppMedicineLibMapping)

    async def batch_save(self, data: List[UpdateOne]):
        await self.bulk_write(data, ordered=False)

    async def get_by_app_id(self, app_id: str):
        query = {
            "app_id": ObjectId(app_id)
        }
        return await self.query_record(query)

    async def add(self, data: ApplicationMedicineLibraryMapPO) -> str:
        values = data.dict(exclude_none=True)
        result = await self.insert(values)
        return str(result)

